package com.jxtti.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.jxtti.entity.ArticleList;
import com.jxtti.entity.SubjectMenu;
import com.jxtti.entity.SubjectMsg;
import com.jxtti.entity.SubjectTemplate;
import com.jxtti.entity.SubjectTopic;
import com.jxtti.service.ArticleListService;
import com.jxtti.service.SubjectMenuService;
import com.jxtti.service.SubjectMsgService;
import com.jxtti.service.SubjectTemplateService;
import com.jxtti.service.SubjectTopicService;
import com.jxtti.utils.CommConstant;

/**
 * 网站前台 首页
 * @author bff
 *
 */

@Controller
@RequestMapping("/WebSubject") 
public class WebSubjectController {
	private final static Logger log= Logger.getLogger(WebSubjectController.class);
	

	@Autowired
	private SubjectTopicService  subjectTopicService; 
		
	@Autowired
	private SubjectMenuService  subjectMenuService; 
	
	@Autowired
	private SubjectMsgService  subjectMsgService; 
	
	@Autowired
	private SubjectTemplateService  subjectTemplateService; 
	
	@Autowired //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private ArticleListService  articleListService; 
	
	
	@InitBinder  //初始化数据的时候，进行数据类型转换，String类型转为Date类型
    public void initBinder(WebDataBinder binder) {     
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");     
        dateFormat.setLenient(false);     
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
	

	@RequestMapping(value = "/subjectMain")
	@ResponseBody
	public ModelAndView subjectMain(@RequestParam Map<String, Object> data,SubjectTopic subjectTopic,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			
			subjectTopic = this.subjectTopicService.querySubjectTopicById(subjectTopic);
			data.put("subjectTopic", subjectTopic);
			mav.addObject("data", data);
			
			SubjectTemplate  subjectTemplate = new SubjectTemplate();
			subjectTemplate.setId(subjectTopic.getTemplate_id());
			subjectTemplate = this.subjectTemplateService.querySubjectTemplateById(subjectTemplate);
			switch (subjectTemplate.getFalg())
			{
		    	case CommConstant.SUBJECT_STJS:
		    		mav.setViewName("/pages/WebSubject/stjszt/index.jsp");
		    		break;
		    	case CommConstant.SUBJECT_WRYJG:
		    		mav.setViewName("/pages/WebSubject/psInfoDisclosure/index.jsp");
		    		break;
		    	case CommConstant.SUBJECT_RED:
					mav.setViewName("/pages/WebSubject/redzt/index.jsp");
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	
	/**
	 * 跳转到具体页页展现.
	 * @param data
	 * @param subjectTopic
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/detailMsgPage")
	@ResponseBody
	public ModelAndView detailMsgPage(@RequestParam Map<String, Object> data,SubjectMsg subjectMsg,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			int menuId = Integer.parseInt(data.get("menu_id").toString());
			SubjectMenu menu = new SubjectMenu();
			menu.setId(menuId);
			menu  = this.subjectMenuService.querySubjectMenuById(menu);
			Map m = new HashMap();
			m.put("id", menu.getId());
			/*	SubjectTopic  subjectTopic = new SubjectTopic();
			subjectTopic.setId(menu.getTopic_id());*/
			SubjectTopic subTop = this.subjectTopicService.queryWithSubjectTopicByMenuId(m);
			data.put("subjectTopic", subTop);
			
			if(menu.getFlag().equals("xzcf"))
			{
				ArticleList entity = new ArticleList();
				entity.setId(Integer.parseInt(data.get("id").toString()));
				entity = this.articleListService.queryById(entity);
				subjectMsg.setAuthor(entity.getAuthor());
				subjectMsg.setContent(entity.getContents());
				subjectMsg.setCreateTime(entity.getCreateDate());
				subjectMsg.setTitle(entity.getTitle());
			}else
			{
				subjectMsg = this.subjectMsgService.querySubjectMsgById(subjectMsg);
			}
			data.put("subjectMsg", subjectMsg);
			mav.addObject("data", data);
			
			Map<String, Object>   d = new HashMap<String, Object> ();
			d.put("id", subjectMsg.getId());
			
			
			switch (subTop.getTemplate_flag())
			{
				case CommConstant.SUBJECT_STJS:
					mav.setViewName("/pages/WebSubject/stjszt/detailMsgPage.jsp");
					break;
				case CommConstant.SUBJECT_WRYJG:
					mav.setViewName("/pages/WebSubject/psInfoDisclosure/detailpage.jsp");
					break;
				case CommConstant.SUBJECT_RED:
					mav.setViewName("/pages/WebSubject/redzt/detailPage.jsp");
					break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	@RequestMapping(value = "/SubMenuPage")
	@ResponseBody
	public ModelAndView SubMenuPage(@RequestParam Map<String, Object> data,SubjectMenu subjectMenu,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			subjectMenu = this.subjectMenuService.querySubjectMenuById(subjectMenu);
			
			
			Map<String, Object>  m = new HashMap<String, Object> ();
			m.put("parent_id", subjectMenu.getId());
			List<SubjectMenu> lstSubjectMenu = this.subjectMenuService.queryAll(m);
			lstSubjectMenu.add(subjectMenu);
			
			//默认为当前页面 。
			data.put("subjectMenu", subjectMenu);
			data.put("lstSubjectMenu", lstSubjectMenu);
			mav.addObject("data", data);
			m.put("id", subjectMenu.getTopic_id());
			SubjectTemplate  subjectTemplate  = this.subjectTemplateService.querySubjectTemplateByTopicID(m);
			SubjectTopic st = new SubjectTopic();
			st.setId(subjectMenu.getTopic_id());
			st = this.subjectTopicService.querySubjectTopicById(st);
			data.put("subjectTopic", st);
			switch (subjectTemplate.getFalg())
			{
				case CommConstant.SUBJECT_STJS:
					mav.setViewName("/pages/WebSubject/stjszt/subMenuPage.jsp");
					break;
				case CommConstant.SUBJECT_WRYJG:
					
					mav.setViewName("/pages/WebSubject/psInfoDisclosure/subMenuPage.jsp");
					break;
				case CommConstant.SUBJECT_RED:
					
					mav.setViewName("/pages/WebSubject/redzt/subPage.jsp");
					break;
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	
	
	@RequestMapping(value="/querySubjectTopic") 
	@ResponseBody
	public List<SubjectTopic>  querySubjectTopic(@RequestParam Map<String, Object> data,  SubjectTopic subjectTopic, HttpServletRequest request,
			HttpServletResponse response) throws Exception{	
		List<SubjectTopic> lstSubjectTopic = subjectTopicService.queryPublishSubjectTopic(data);
		
		return lstSubjectTopic;
	}
	
	
	@RequestMapping(value="/getSubjectTopicById") 
	@ResponseBody
	public SubjectTopic  getSubjectTopicById(@RequestParam Map<String, Object> data,  SubjectTopic subjectTopic, HttpServletRequest request,
			HttpServletResponse response) throws Exception{	
		
		SubjectTopic st= subjectTopicService.querySubjectTopicById(subjectTopic);
		
		return st;
	}
		
	
	/**
	 * 加载栏目菜单.
	 * @param data
	 * @param subjectTopic
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/loadSubjectMenus") 
	@ResponseBody
	public List<SubjectMenu>  loadSubjectMenus(@RequestParam Map<String, Object> data,  SubjectMenu subjectMenu, HttpServletRequest request,
			HttpServletResponse response) throws Exception{	
		List<SubjectMenu> st= subjectMenuService.queryListByOrderSeq(data);
		return st;
	}
	
	
	
	@RequestMapping(value="/loadSubjectMsg") 
	@ResponseBody
	public List<SubjectMsg>  loadSubjectMsg(@RequestParam Map<String, Object> data,  SubjectMenu subjectMenu, HttpServletRequest request,
			HttpServletResponse response) throws Exception{	
		List<SubjectMsg> st= this.subjectMsgService.loadSubjectMsgTitle(data);
		return st;
	}
	
	
	
	
	@RequestMapping(value="/loadSubjectMsgByFlag") 
	@ResponseBody
	public List<SubjectMsg>  loadSubjectMsgByFlag(@RequestParam Map<String, Object> data,  SubjectMenu subjectMenu, HttpServletRequest request,
			HttpServletResponse response) throws Exception{	
		List<SubjectMsg> st = null;
		
		if(data.get("flag") != null && data.get("flag").equals("xzcf"))  //如果标签是行政处罚， 数据源从网站中获取  。 
		{
			Map<String, Object> m = new HashMap();
			m.put("is_deleted", "0");
			m.put("status", "2");//状态 草稿0,待审1,审核通过2,审核失败3
			m.put("articleSite", "'0','2'");//文章所属站点 0 外网,1内网，2内外网		
			m.put("issue", "1");//是否网页显示
			m.put("categoryID", "72");    //从网站中Copy中的编码.
			m.put("topNum", "5");
			
			List<ArticleList> lst = articleListService.queryListForIndex(m);
			st = new ArrayList();
			for(ArticleList art : lst)
			{
				SubjectMsg msg = new SubjectMsg();
				msg.setAuthor(art.getAuthor());
				msg.setContent(art.getContents());
				msg.setCreateTime(art.getCreateDate());
				msg.setId(art.getId());
				msg.setImg(art.getImagesPath());
				msg.setTitle(art.getTitle());
				msg.setFlag("xzcf");
				st.add(msg);
			}
			
			//不合并本身专题 的内容.
			/*List<SubjectMsg> tst= this.subjectMsgService.loadSubjectMsgByFlag(data);
			st.addAll(tst);*/
			
		}else
		{
			 st= this.subjectMsgService.loadSubjectMsgByFlag(data);
		}
		return st;
	}
	
	
	
	/**
	 * 子导航页面， 行政处罚数据为网站中的数据.
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/querySubjectMsgPage")
	@ResponseBody
	public Map<String, Object> querySubjectMsgPage(@RequestParam Map<String, Object> data,
			HttpServletRequest request,	HttpServletResponse response) throws Exception{	
		Map<String, Object> result = new HashMap<String, Object>();	
	
		
		//如果标识为行政处罚. 到网站取数据.
		if(data.get("flag") != null && data.get("flag").equals("xzcf"))
		{
			 /*currPageNum=1, pageRowSize=10, firstResult=0, maxResult=10,*/
			Map<String, Object> m = new HashMap();
			m.put("is_deleted", "0");
			m.put("status", "2");//状态 草稿0,待审1,审核通过2,审核失败3
			m.put("articleSite", "'0','2'");//文章所属站点 0 外网,1内网，2内外网		
			m.put("issue", "1");//是否网页显示
			m.put("categoryID", "72");    //从网站中Copy中的编码.
			m.put("currPageNum", data.get("currPageNum"));
			m.put("pageRowSize", data.get("pageRowSize"));
			m.put("firstResult", data.get("firstResult"));
			m.put("maxResult", data.get("maxResult"));
			//data.put("topNum", "5");
			
			PageInfo<ArticleList> pages = articleListService.listPage(m);
			List<ArticleList> lst = pages.getList();
			
			List<SubjectMsg> lstmsg = new ArrayList();
			for(ArticleList art : lst)
			{
				SubjectMsg msg = new SubjectMsg();
				msg.setAuthor(art.getAuthor());
				msg.setContent(art.getContents());
				msg.setCreateTime(art.getCreateDate());
				msg.setId(art.getId());
				msg.setImg(art.getImagesPath());
				msg.setTitle(art.getTitle());
				msg.setFlag("xzcf");
				msg.setMenu_id(Integer.parseInt(data.get("id").toString()));
				lstmsg.add(msg);
			}
			result.put("total", pages.getTotal());
			result.put("rows", lstmsg);	
			
		}
		else
		{
			
			PageInfo<SubjectMsg> page = subjectMsgService.listPage(data);
			result.put("total", page.getTotal());
			result.put("rows", page.getList());			
		}
		return result;
	}
	
	
	
	/**
	 * 查询置顶文章.
	 * @param data
	 * @param subjectMenu
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	
	
	@RequestMapping(value="/queryArticleByTop") 
	@ResponseBody
	public List<SubjectMsg>  queryArticleByTop(@RequestParam Map<String, Object> data,  SubjectMenu subjectMenu, HttpServletRequest request,
			HttpServletResponse response) throws Exception{	
		List<SubjectMsg> st= this.subjectMsgService.queryArticleByTop(data);
		return st;
	}
	
	
	@RequestMapping(value="/loadTreeMenus") 
	@ResponseBody
	public List<SubjectMenu>  loadTreeMenus(@RequestParam Map<String, Object> data,  SubjectMenu subjectMenu, HttpServletRequest request,
			HttpServletResponse response) throws Exception{	
		List<SubjectMenu> st= subjectMenuService.queryMenuTree(data);
		return st;
	}
	
	
	
	
	
}
